﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Common;

public partial class System_userlist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin("system user");

        switch (RequestBase.GetFormString("otype"))
        {
            case "loaddata":
                LoadingData();
                break;
            case "delete":
                Delete();
                break;
        }
    }
 
    #region 信息绑定 
    void LoadingData()
    {
        string ctrl = "~/admin/system/_controls/ctlUsersList.ascx";

        StringBuilder SQL = new StringBuilder().Append(" AND DeletionStateCode = 0 ");
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("keys")))
        {
            SQL.Append(" AND (Username LIKE '%" + RequestBase.GetFormString("keys") + "%' OR FullName LIKE '%" + RequestBase.GetFormString("keys") + "%')");
        } 
         
        string SQL_Sort = "";
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("field")))
        {
            SQL_Sort = " ORDER BY " + RequestBase.GetFormString("field") + " " + RequestBase.GetFormString("level") + " ";
        }
        else
        {
            SQL_Sort = "  ORDER BY BusinessUnitName ASC, FullName ASC ";
        }

        Control uc_rep = LoadControl(ctrl);

        Repeater rptUsersList = (Repeater)uc_rep.FindControl("rptUsersList");

        DBUtility.Pagination pagination = new DBUtility.Pagination();
        try
        {
            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = SQL_Sort;
            pagination.Primarykey = "SystemUserId";
            pagination.SearchField = "*";
            pagination.TableName = "vw_System_UserBase";
            pagination.PageSize = 30;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.ToString();

            rptUsersList.DataSource = pagination.GetDataTable();
            rptUsersList.DataBind();

            pagination.PageBarFerrari();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptUsersList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 删除用户信息 
    void Delete()
    {
        string res = "";

        if (Validate("system user", 3))
        {
            res = BLL.SystemUserBase.Delete(RequestBase.GetFormString("userid"), false); 
        }
        else
        {
            res = "You does not delete permissions!";
        }
        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}
